package com.hs.mysql.proxy.service;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hs.mysql.proxy.annotation.DataSourceSelector;
import com.hs.mysql.proxy.enums.DynamicDataSourceEnum;
import com.hs.mysql.proxy.mapper.UserMapper;
import com.hs.mysql.proxy.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: UserService
 * @author: hs
 * @date: 2020/5/8 11:42
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @DataSourceSelector(value = DynamicDataSourceEnum.SLAVE)
    public List<User> listUser() {

        EntityWrapper<User> wrapper = new EntityWrapper();

        List<User> users = userMapper.selectList(wrapper);

        return users;
    }

    @DataSourceSelector(value = DynamicDataSourceEnum.MASTER)
    public int update() {
        User user = new User();
        user.setUserId(Long.parseLong("1196978513958141952"));
        user.setUserName("修改后的名字2");
        return userMapper.updateById(user);
    }

    @DataSourceSelector(value = DynamicDataSourceEnum.SLAVE)
    public User find() {

        return userMapper.selectById(Long.parseLong("1196978513958141952"));
    }
}
